﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class WeaponSkillTable
    {
        private static Logger _log = Logger.getLogger( typeof( WeaponSkillTable ).FullName );

        private static WeaponSkillTable _instance;

        private Dictionary<int, L1WeaponSkill> _weaponIdIndex = new Dictionary<int, L1WeaponSkill>();

        public static WeaponSkillTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new WeaponSkillTable();
            }
            return _instance;
        }

        private WeaponSkillTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM weapon_skill";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    int weaponId = SqlUtils.toInt( item["weapon_id"] );
                    int probability = SqlUtils.toInt( item["probability"] );
                    int fixDamage = SqlUtils.toInt( item["fix_damage"] );
                    int randomDamage = SqlUtils.toInt( item["random_damage"] );
                    int area = SqlUtils.toInt( item["area"] );
                    int skillId = SqlUtils.toInt( item["skill_id"] );
                    int skillTime = SqlUtils.toInt( item["skill_time"] );
                    int effectId = SqlUtils.toInt( item["effect_id"] );
                    int effectTarget = SqlUtils.toInt( item["effect_target"] );
                    bool isArrowType = Convert.ToBoolean( item["arrow_type"] );
                    int attr = SqlUtils.toInt( item["attr"] );
                    L1WeaponSkill weaponSkill = new L1WeaponSkill( weaponId, probability,
                            fixDamage, randomDamage, area, skillId, skillTime, effectId,
                            effectTarget, isArrowType, attr );
                    _weaponIdIndex[weaponId] = weaponSkill;
                }
            }
            _log.config( "武器スキルリスト " + _weaponIdIndex.Count + "件ロード" );
        }

        public L1WeaponSkill getTemplate( int weaponId )
        {/*TTOK*/
            L1WeaponSkill ret;
            _weaponIdIndex.TryGetValue( weaponId, out ret );
            return ret;
        }
    }
}
